package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.MediaQueryDTO;
import com.jeff.tianti.cms.entity.Media;
import com.jeff.tianti.cms.service.MediaService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/media")//媒体供需
public class MediaController {
    @Autowired
    private MediaService mediaService;
    @Autowired
    private ColumnInfoCom columnInfoCom;

    /**
     * @Ceator:liuYI
     * @Describe:媒体供需列表查询
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");//当前页
        String pageSizeStr = request.getParameter("pageSize");//每页显示条数
        String auditFlag = request.getParameter("auditFlag");//审核标记
        String mediaType = request.getParameter("mediaType");//cms_column_info主键id
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        MediaQueryDTO mediaQueryDTO = new MediaQueryDTO();
        mediaQueryDTO.setPageSize(pageSize);
        mediaQueryDTO.setCurrentPage(currentPage);
        mediaQueryDTO.setMediaType(mediaType);
        mediaQueryDTO.setAuditFlag(auditFlag);
        PageModel<Media> page = mediaService.queryMediaPage(mediaQueryDTO);
        Map map = columnInfoCom.findColumnInfoList(Constants.COLUMNINFO_MTGX_CODE);
        model.addAttribute("map", map);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("mediaQueryDTO", mediaQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.THE_MEDIA_OF_SUPPLY_AND_DEMAND);
        return "user/media/media_list";
    }

    /**
     * @Ceator:liuYI
     * @Describe:媒体供需详情数据展示
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        Media media = new Media();
        if (StringUtils.isNotBlank(id)) {
            media = mediaService.find(id);
        }
        model.addAttribute("media", media);
        model.addAttribute(Constants.MENU_NAME, Constants.THE_MEDIA_OF_SUPPLY_AND_DEMAND);
        return "user/media/edit/media_edit";
    }

    /**
     * @Ceator:liuYI
     * @Describe:媒体供需详审核操作
     */
    @RequestMapping("/auditFlag")
    @ResponseBody
    public AjaxResult auditFlag(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String auditFlag = request.getParameter("auditFlag");
            if (StringUtils.isNotBlank(id)) {
                Media media = mediaService.find(id);
                media.setAuditFlag(auditFlag);
                mediaService.update(media);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
